package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.CouponBiz;
import com.coolbi.onsupai.core.constant.CouponConstant;
import com.coolbi.onsupai.core.entity.Coupon;
import com.coolbi.security.web.action.SessionAction;

/**
 * @description:优惠劵Action
 * @author： cgn
 * @createdate： 2012-7-8
 */
public class CouponAction extends SessionAction {

	private static final long serialVersionUID = 1L;
	private Coupon coupon;
	@Autowired
	private CouponBiz couponBiz;
	private List<Coupon> couponList;

	/**
	 * 优惠券使用
	 * @return
	 */
	public String checkCoupon() {
		try {
			coupon = couponBiz.findByCouponNo(coupon.getCouponNo(), coupon.getCustomerId(), CouponConstant.UNUSE);
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("{'status': 'error', 'result': '" + e.getMessage() + "'}");
		}
		return ajax("{'status': 'success', 'result': " + coupon.getEquivalence() + "}");
	}
	
	/**
	 * @return 添加优惠劵信息
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			couponBiz.save(coupon);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * @return 更新优惠劵信息
	 * @throws Exception
	 */
	public String update() throws Exception {
		try {
			couponBiz.update(coupon);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * @return 删除优惠劵信息
	 * @throws Exception
	 */
	public String delete() throws Exception {
		try {
			couponBiz.delete(coupon.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 根据Id查看优惠劵信息
	 * 
	 * @return
	 */
	public String findById() {
		try {
			coupon = couponBiz.get(coupon.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 查找优惠劵信息并分页
	 * 
	 * @return
	 */
	public String listByPage() {
		try {
			pager.setRecordCount(couponBiz.count());
			couponList = couponBiz.listByPage((pager.getCurrPageNo() - 1)
					* pager.getPageSize(), pager.getPageSize());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public Coupon getCoupon() {
		return coupon;
	}

	public void setCoupon(Coupon coupon) {
		this.coupon = coupon;
	}

	public List<Coupon> getCouponList() {
		return couponList;
	}

	public void setCouponList(List<Coupon> couponList) {
		this.couponList = couponList;
	}

	public void setCouponBiz(CouponBiz couponBiz) {
		this.couponBiz = couponBiz;
	}

}
